package com.jeesite.modules.storage.util;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.CertAlipayRequest;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.*;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayFundTransUniTransferRequest;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayFundTransUniTransferResponse;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import com.jeesite.common.config.Global;

import java.util.HashMap;
import java.util.Map;

public class AlipayUtil {

    private static String appId = Global.getConfig("storage.alipay.appId");
    private static String privateKey = Global.getConfig("storage.alipay.privateKey");
    private static String alipayPublicKey = Global.getConfig("storage.alipay.alipayPublicKey");

    private static String AlipayPublicCertPath = Global.getConfig("storage.alipay.cert.alipayPublicCertPath");//支付宝公钥证书路径
    private static String AppCertPath = Global.getConfig("storage.alipay.cert.appCertPath");//商户公钥证书路径
    private static String RootCertPath = Global.getConfig("storage.alipay.cert.rootCertPath");//根证书路径

    private static String url = "https://openapi.alipay.com/gateway.do";

    private static String payNotify = Global.getConfig("storage.alipay.notify.pay");
    private static String refundNotify = Global.getConfig("storage.alipay.notify.refund");
    private static String transferNotify = Global.getConfig("storage.alipay.notify.transfer");

    public static Boolean alipaySignature(Map<String, String> params) throws AlipayApiException {
        return AlipaySignature.certVerifyV1(params, AlipayPublicCertPath, "utf-8", "RSA2");
    }

    /*
    * APP发起支付
    * @return app_id=2021001114684488&biz_content=%7B%22timeout_express%22%3A%2210m%22%2C%22total_amount%22%3A%220.01%22%2C%22body%22%3A%22alipay%22%2C%22subject%22%3A%22%E6%B5%8B%E8%AF%95%E6%94%AF%E4%BB%98%E5%AE%9DAPP%E6%94%AF%E4%BB%98%22%2C%22out_trade_no%22%3A%2215808727579821%22%2C%22time_expire%22%3A%222020-02-05+11%3A29%22%2C%22goods_type%22%3A%221%22%2C%22specified_channel%22%3A%22pcredit%22%7D&charset=utf-8&format=JSON&method=alipay.trade.app.pay&notify_url=http%3A%2F%2Fweb.storage.yuxin.fit&return_url=http%3A%2F%2Fweb.storage.yuxin.fit&sign_type=RSA2&timestamp=2020-02-05+11%3A19%3A17&version=1.0&sign=M%2BTxb8Ct6l7komsYVQtPrIzCvseQ9twz%2F2kpWJIUP9WndnhIQx0DgXLfNZkhpTv6bZV9bV%2BqyqIleBAxqNsoOYZHUjA6QToLNqFgfeXUUb22hJqPf9TQJfxDE8P6qwaoVzzRA8nxHZvYDTzPaJKNXmCmw2eRxxyPk06F4AXtBeFcWn6v2RriUTc1fe3A36GEINMiBGGhzqZBEquEo4EkhLtFEpnX%2Fo8B7UCFfi4iJE29kosfecC3KJ1jX0W6XabXK8PWNchX894u34PoNIeHahlK5ihGrKSql695B60klLq5SdABblq1n1yqfRx2Vg9CmN7aJbYw%2BaXseVVPVNBi1A%3D%3D
    * */
    public static String createAppPay(String outTradeNo,String subject,Double amount) throws AlipayApiException {
        amount = 0.01d;
        AlipayClient alipayClient = new DefaultAlipayClient(url,appId,privateKey,"json","utf-8",alipayPublicKey,"RSA2");
        AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
        AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
        model.setSubject(subject);
        model.setOutTradeNo(outTradeNo);
        model.setTotalAmount(String.valueOf(amount));
        request.setBizModel(model);
        request.setNotifyUrl(payNotify);
        AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
        return response.isSuccess() ? response.getBody() : "";
    }

    public static void createH5Pay() throws AlipayApiException {
        CertAlipayRequest request = new CertAlipayRequest();
        request.setAlipayPublicCertPath(AlipayPublicCertPath);
        request.setCertPath(AppCertPath);
        request.setRootCertPath(RootCertPath);
        request.setAppId(appId);
        request.setCharset("utf-8");
        request.setPrivateKey(privateKey);
        request.setFormat("json");
        request.setSignType("RSA2");
        request.setServerUrl(url);
        AlipayClient alipayClient = new DefaultAlipayClient(request);


        AlipayTradeWapPayRequest wapPayRequest = new AlipayTradeWapPayRequest();
        AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();
        model.setBody("H5支付测试");
        model.setSubject("H5支付测试");
        model.setOutTradeNo("test001");          //商户订单号(自动生成)
        model.setTotalAmount("0.01");
        model.setProductCode("QUICK_WAP_WAY");
        String quitUrl="";//用户付款中途退出返回商户网站的地址
        model.setQuitUrl(quitUrl);
        wapPayRequest.setApiVersion("1.0");
        wapPayRequest.setBizModel(model);
        wapPayRequest.setNotifyUrl(payNotify);          //回调地址
        AlipayTradeWapPayResponse response = alipayClient.sdkExecute(wapPayRequest);
    }

    public static Boolean refund(String outTradeNo,String reason, Double amount) throws AlipayApiException {
        AlipayClient alipayClient = new DefaultAlipayClient(url,appId,privateKey,"json","GBK",alipayPublicKey,"RSA2");
        AlipayTradeRefundModel model = new AlipayTradeRefundModel();
        model.setOutTradeNo(outTradeNo);
        model.setRefundAmount(String.valueOf(amount));
        model.setRefundReason(reason);
        AlipayTradeRefundRequest tradeRefundRequest =new AlipayTradeRefundRequest();
        tradeRefundRequest.setBizModel(model);
        tradeRefundRequest.setNotifyUrl(refundNotify);
        AlipayTradeRefundResponse response = alipayClient.execute(tradeRefundRequest);
        return response.isSuccess();
}

    public static void transfer() throws AlipayApiException {

        CertAlipayRequest request = new CertAlipayRequest();
        request.setAlipayPublicCertPath(AlipayPublicCertPath);
        request.setCertPath(AppCertPath);
        request.setRootCertPath(RootCertPath);
        request.setAppId(appId);
        request.setCharset("utf-8");
        request.setPrivateKey(privateKey);
        request.setFormat("json");
        request.setSignType("RSA2");
        request.setServerUrl(url);
        AlipayClient alipayClient = new DefaultAlipayClient(request);

        AlipayFundTransUniTransferRequest transferRequest = new AlipayFundTransUniTransferRequest();
        AlipayFundTransUniTransferModel model = new AlipayFundTransUniTransferModel();
        transferRequest.setApiVersion("1.0");
        //CreateTransferRequesetModel model = new CreateTransferRequesetModel();
        model.setOutBizNo("test001");
        model.setBizScene("DIRECT_TRANSFER");
        model.setOrderTitle("转账测试");
        model.setProductCode("TRANS_ACCOUNT_NO_PWD");
        model.setTransAmount("0.01");
        model.setRemark("test");
        Participant payeeInfo = new Participant();
        payeeInfo.setIdentity("1656755298@qq.com");
        payeeInfo.setIdentityType("ALIPAY_LOGON_ID");
        payeeInfo.setName("吴争辉");
        model.setPayeeInfo(payeeInfo);
        transferRequest.setBizModel(model);
        AlipayFundTransUniTransferResponse response = alipayClient.execute(transferRequest);
    }

    public static boolean AlipaySignature(Map<String, String> params) throws AlipayApiException {
        boolean Signature = AlipaySignature.certVerifyV1(params, AlipayPublicCertPath, "utf-8", "RSA2");
        return Signature;
    }
}
